﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2010, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace Streambolics
{
    /// <summary>
    ///     A Message Port that binds to a serial port.
    /// </summary>

    public class SerialMessagePort: ComputingModule, IMessageProducer, IMessageConsumer, ISignalProducer
    {
        private SerialPort _Port;

        public SerialMessagePort ()
            : this (new SerialPort())
        {
        }

        public SerialMessagePort (string aPortName)
            : this (new SerialPort (aPortName))
        {
        }

        /// <summary>
        ///     Creates a SerialMessagePort and binds it to a given serial port.
        /// </summary>
        /// <param name="aPort">
        ///     The serial port to bind to.
        /// </param>

        public SerialMessagePort (SerialPort aPort)
        {
            _Port = aPort;
            // TODO : Bind to the events of the port.
        }

        /// <summary>
        ///     The SerialPort that implements the communication.
        /// </summary>

        public SerialPort Implementation
        {
            get
            {
                return _Port;
            }
        }

        /// <summary>
        ///     The DoStart method opens the port and declares the
        ///     module started.
        /// </summary>
        /// <remarks><para>
        ///     A subclass might want to redefine this function to
        ///     configure the port prior to opening.
        /// </para></remarks>

        protected override void DoStart ()
        {
            if (!_Port.IsOpen)
            {
                _Port.Open ();
            }

            // TODO : Do not declare the port open immediately.
            //        Wait for a function to do so.
            if (_Port.IsOpen)
            {
                DeclareStarted ();
            }
            else
            {
                DeclareFailed (new Exception ("Serial port did not open"));
            }
        }

        protected override void DoStop ()
        {
            if (_Port.IsOpen)
            {
                _Port.Close ();
            }

            DeclareStopped ();
        }

        #region IMessageProducer Members

        public byte[] ProduceMessage ()
        {
            if (_Port.IsOpen)
            {
                int n = _Port.BytesToRead;
                if (n > 0)
                {
                    byte[] m = new byte[n];
                    _Port.Read (m, 0, n);
                    return m;
                }
            }
            return null;
        }

        #endregion

        #region IMessageConsumer Members

        public void ConsumeMessage (byte[] aMessage)
        {
            // TODO : Investigate what to do when the message cannot be sent.
            if (_Port.IsOpen)
            {
                _Port.Write (aMessage, 0, aMessage.Length);
            }
        }

        #endregion

        #region ISignalProducer Members

        public SignalEvent ProduceSignal ()
        {
            // TODO : generate a signal when the status lines have changed.
            return null;
        }

        #endregion
    }
}
